# 使用Python官方镜像作为基础
FROM python:3.10-slim

# 设置工作目录
WORKDIR /app

# 先安装系统依赖和supervisor，利用Docker缓存
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    supervisor \
    && rm -rf /var/lib/apt/lists/*

# 安装Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && \
    apt-get install -y --no-install-recommends nodejs && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 安装Python依赖
COPY requirements.txt .
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir -r requirements.txt

# 复制项目代码
COPY . .

# 修复：先将宿主机的unionpay-mcp-server复制到项目目录，再通过相对路径复制到容器
# 注意：构建前需手动执行：cp -r /usr/local/lib/node_modules/unionpay-mcp-server ./ (在宿主机项目根目录)
COPY ./unionpay-mcp-server /usr/local/lib/node_modules/unionpay-mcp-server

# 处理unionpay-mcp-server
RUN ln -sf /usr/local/lib/node_modules/unionpay-mcp-server/dist/index.js /usr/local/bin/unionpay-mcp-server && \
    # 给实际执行文件添加可执行权限
    chmod +x /usr/local/lib/node_modules/unionpay-mcp-server/dist/index.js

# 暴露代理服务端口
EXPOSE 20011 20012 9001

# 环境变量配置（移除敏感信息，建议运行时传入）
#ENV HOME HOME=/app \
#    UP_ACCESS_TYPE=01 \
#    UP_MER_ID=777290058211279 \
#    UP_TOKEN_TYPE=01 \
#    UP_SIGN_CERT_PATH=/app/cert/acp_test_sign.pfx \
#    UP_SIGN_CERT_PWD=000000 \
#    UP_VALIDATE_CERT_DIR=/app/cert/verify_sign_acp.cer

# 配置supervisord
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# 启动命令
CMD ["/usr/bin/supervisord"]
